<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('客人信息列表')"/>
    <style>
        #serviceItemsTable {
            width: 80%;
            margin: 0 auto;
            font-family: Arial, sans-serif;
            border-collapse: collapse;
            border: 2px solid #333;
            background-color: #f9f9f9;
            table-layout: fixed;
        }

        #serviceItemsTable th,
        #serviceItemsTable td {
            padding: 15px;
            text-align: left;
            border: 1px solid #ddd;
        }

        #serviceItemsTable th {
            background-color: #4CAF50;
            color: #fff;
            font-weight: bold;
            border-bottom: 2px solid #333;
        }

        #serviceItemsTable th:nth-child(1),
        #serviceItemsTable th:nth-child(2),
        #serviceItemsTable th:nth-child(3),
        #serviceItemsTable th:nth-child(4) {
            width: 25%;
        }

        #serviceItemsTable tbody tr {
            height: auto;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>客人姓名：</label>
                            <input type="text" name="name"/>
                        </li>
                        <li>
                            <label>联系电话：</label>
                            <input type="text" name="phone"/>
                        </li>
                        <li>
                            <label>入住时间：</label>
                            <input type="text" class="time-input" placeholder="请选择入住时间" name="checkInTime"/>
                        </li>
                        <li>
                            <label>退房时间：</label>
                            <input type="text" class="time-input" placeholder="请选择退房时间" name="checkOutTime"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <!-- 退房模态框 -->
        <div class="modal fade" id="checkoutModal" tabindex="-1" role="dialog" aria-labelledby="checkoutModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="checkoutModalLabel">退房消费详情</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <p>房费: <span id="roomPrice"></span></p>
                                <p>入住时间: <span id="checkInTime"></span></p>
                                <p>退房时间: <span id="checkOutTime"></span></p>
                                <p>房费总计: <span id="roomTotalPrice"></span></p>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <table class="table table-bordered">
                                    <thead>
                                    <tr>
                                        <th style="width: 25%">服务项目</th>
                                        <th style="width: 25%">数量</th>
                                        <th style="width: 25%">单价</th>
                                        <th style="width: 25%">小计</th>
                                    </tr>
                                    </thead>
                                    <tbody id="serviceItemsBody">
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12 text-right">
                                <p>总计: <span id="totalPrice"></span></p>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" onclick="confirmCheckout()">确认退房</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:guest:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
               shiro:hasPermission="system:guest:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="system:guest:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:guest:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:guest:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:guest:remove')}]];
    var statusDatas = [[${@dict.getType('customer_status')}]];
    var prefix = ctx + "system/guest";
    var getServiceItemTotalPriceUrl = ctx + "system/consumption/getServiceItemTotalPrice";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "客人信息",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'name',
                    title: '客人姓名'
                },
                {
                    field: 'idNumber',
                    title: '身份证号'
                },
                {
                    field: 'phone',
                    title: '联系电话'
                },
                {
                    field: 'roomName',
                    title: '房间号',
                    formatter: function (value, row, index) {
                        return value + '-房';
                    }
                },
                {
                    field: 'checkInTime',
                    title: '入住时间'
                },
                {
                    field: 'checkOutTime',
                    title: '退房时间'
                },
                {
                    field: 'totalPrice',
                    title: '总费用',
                    formatter: function (value, row, index) {
                        return value + "元";
                    }
                },
                {
                    field: 'status',
                    title: '客人状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="$.operate.checkout(\'' + row.id + '\', \'' + row.roomId + '\', \'' + row.roomName + '\', \'' + row.checkInTime + '\', \'' + row.checkOutTime + '\', \'' + row.totalPrice + '\')"><i class="fa fa-sign-out"></i>退房</a> ');
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });

    $.operate.checkout = function(id, roomId, roomNumber, checkInTime, checkOutTime, roomTotalPrice) {
        var serviceTotalPriceDTO = {
            custmerId: id,
            roomId: roomId
        };
        $.post(getServiceItemTotalPriceUrl, serviceTotalPriceDTO, function(r) {
            console.log("返回结果：=====>", r);
            if (r.code === 0) {
                var data = r.data;
                var itemVoList = data.itemVoList;
                var totalPrice = data.totalPrice;

                // 填充模态框内容
                $("#roomNumber").text(roomNumber);
                $("#checkInTime").text(checkInTime);
                $("#checkOutTime").text(checkOutTime);
                $("#roomTotalPrice").text(roomTotalPrice + "元");

                // 清空tbody内容
                $("#serviceItemsBody").empty();
                itemVoList.forEach(function(item) {
                    console.log("item:=====>", item);
                    var $newRow = $("<tr>");
                    $newRow.append($("<td>", { style: "width: 25%", text: item.serviceItemName }));
                    $newRow.append($("<td>", { style: "width: 25%", text: item.quantity }));
                    $newRow.append($("<td>", { style: "width: 25%", text: item.price + "元" }));
                    $newRow.append($("<td>", { style: "width: 25%", text: (item.price * item.quantity) + "元" }));
                    $("#serviceItemsBody").append($newRow);
                });

                $("#totalPrice").text((Number(totalPrice) + Number(roomTotalPrice)) +  "元");

                // 显示模态框
                $("#checkoutModal").modal("show");
            } else {
                $.modal.alertError(r.msg);
            }
        });
    };

    function confirmCheckout() {
        // 在这里添加确认退房的具体逻辑
        alert("确认退房操作");
    }
</script>
</body>
</html>